<!-- 微信登录 -->
<template>
	<view class="wx-login">
		<image style="width: 200rpx;height: 200rpx;margin-bottom: 40rpx;" :src="images.logo"></image>
		<!-- <view class="btn-login" @click="handleLogin">微信授权登录</view> -->
		<button open-type="liveActivity" class="btn-login" @click="handleLogin" @createliveactivity="getSubscriptionMsg">微信授权登录</button>
	</view>
</template>

<script>
	// 引入vuex
	import {
		mapState,
		mapGetters,
		mapMutations,
		mapActions
	} from 'vuex'
	
	// 公共方法
	import cookie from '@/utils/cookie.js'


	// 引入图片
	import imgs from '@/static/images/index.js'
	
	// api引入
	import {
		getLogin, // 微信登录
	} from '@/api/user.js'

	export default {
		props: {
			/**
			 * 订阅消息
			 */
			getRequestMsg:{
				type: Function,
				default: ()=>{}
			},

			/**
			 * 是否勾选同意协议
			 */
			agreement: {
				type: Boolean,
				default: false
			}
		},
		data() {
			return {
				images: {}, // 图片组
			};
		},
		created() {
			this.images = {
				...imgs
			}
		},
		methods: {
			...mapMutations(['setUserInfo']),
			
			/**
			 * 点击获取订阅
			 */
			getSubscriptionMsg(e){
			console.log(e, 'mmmmmmm');
			},
			
			

			/**
			 * 点击用户登录
			 */
			async handleLogin() {
				
				// 调起订阅通知
				this.$emit('getRequestMsg')

				// 先判断勾选协议了没
				if (!this.agreement) {
					this.$public.showText('请先阅读并同意用户协议和隐私协议')
					return
				}
				



				let that = this
				let loginValue = await new Promise(function(resolve, reject) {
					wx.login({
						timeout: 3000,
						success: res => {
							resolve(res)
						},
						fail: err => {
							that.$public.showText(err.errMsg || '登录失败')
							reject(err)
							console.log(err, '登录失败');
						}
					})
				})
				
				console.log('运行了吗2222222',loginValue);
				
				let params = {
					code: loginValue.code,
					invitationCode: cookie.get('invitationCode') || that.invitationCode,
					type: 2
				}

					console.log('运行了吗112222222221',params);
				try {
					console.log('运行了吗111',params);
					const res = await getLogin(params)
					
					console.log('运行了吗',res);

					// 返回错误,如果是701和702就去填信息
					if (res.code !== 200) {
						if (Math.floor(res.code / 100) === 7) {
							uni.redirectTo({
								url: '/pages/me/followWebsite/followWebsite?type=' + loginValue.code
							});
						}
						return
					}

					// 是否艺人，有电话,userArtistVo也不为空就是
					if (this.$public.getValueStatus(res.data) && this.$public.getValueStatus(res.data.phone) && res
						.data.userArtistVo !== null) {
						// 用户参数
						this.setUserInfo(res.data)
						cookie.set('token', res.data?.loginToken || '')


						// 如果有电话就运行
						uni.switchTab({
							url: '/pages/homeTab/index'
						});
					} else {
						uni.redirectTo({
							url: '/pages/me/followWebsite/followWebsite'
						});
					}

				} catch (e) {
					that.$public.showText(e.errMsg || '登录失败')
					console.log('登录请求失败', e);
				}
				
				
				
			},
		}
	}
</script>

<style lang="less" scoped>
	.wx-login {
		#padding(200rpx 0 0 0);
		#flex(column, -, center);

		.btn-login {
			#box(600rpx, -, 20rpx, rgb(88, 190, 106));
			#padding(20rpx 0);
			text-align: center;
			color: #FFFFFF;
			margin-bottom: 20rpx;
		}
	}
</style>